package com.ruozedata.bigdata.lesson05

import scala.collection.mutable
import scala.collection.mutable.{ArrayBuffer, ListBuffer}

/**
 * 集合
 * 定长 Array
 * 变长 ArrayBuffer
 *
 * List: 可以重复
 * ListBuffer
 *
 * Set: 不重复
 *
 * TUPLE
 */
object ArrayApp {

    def main(args: Array[String]): Unit = {

        val array = Array[Int](1,2,3,4)
        val a1 = array :+ 111
        val a2 = 111 +: array


        val array2 = Array[Int](3,4)
        array ++ array2
        array.++(array2)
        println(array)

        for (elem <- array.reverse) {
            println(elem)
        }

        array.foreach(println)

        for (i<-array.length-1 to 0 by -1) {
            print(i)
        }

        ///////////
        val c = Array(1,2,3,4,5)
        c.max
        c.min
        c.sum

        c.mkString(",")
        c.mkString("<",",",">")

        println()
        // mutable
        val d  = ArrayBuffer[Int]()
        d += 1
        d += 2
        d += (3,4,5)
        d ++= Array(7,8)
        d.insert(0, 0)

        // 删除
        d.remove(1)
        d.remove(2)
        d.trimEnd(2)
        d.trimStart(1)
        d.toArray
        c.toBuffer
        println(d)


        // Nil 是一个空List
        val l = List(1,2,3,4,5)
        println(l.head, l.tail)
        4 +: l
        val l4 = 1 :: 2 :: 3 :: Nil
        println(l ::: l4)

        val listBuffer = ListBuffer[Int]()
        listBuffer += 1
        listBuffer += 2
        listBuffer += 3
        listBuffer ++= List(4,5,6,7)
        println(listBuffer)
        listBuffer -= 1
        listBuffer -= 3
        listBuffer -= (4,5)
        listBuffer --= List(7,8)
        println(listBuffer)
        listBuffer.toList
        listBuffer.isEmpty
        listBuffer.head
        listBuffer.tail
        listBuffer.tail.tail
        listBuffer.last
        listBuffer.product
        listBuffer.dropRight(1)


        val l11 = List(1,2,3,4,5,6,7)
        val l22 = List(4,5,6,7,8,9,10)

        l11.union(l22)
        println(l11.intersect(l22))
        println(l11.diff(l22))
        val iterator: Iterator[List[Int]] = l11.sliding(2, 3)
        for (elem <- iterator) {
            println(elem)
        }


        // Set
        val s1 = Set(1,2,3,4,5)
        val ll = List(2,3,4).toSet
        val s2 = mutable.Set()
        //s2 += 1
        //s2 += 2
        //s2 += 3
        //
        //s2 -= 3
        //s2.remove(1)

        val s01 = mutable.Set(1,2,3,4,5,6,7)
        val s02 = mutable.Set(4,5,6,7,8,9,10)
        s01.union(s02)
        s01.++(s02)
        s01 ++ s02
        s01 | s02

        s01.intersect(s02)
        s01 & s02

        s01.diff(s02)


        // Tuple
        val t = 0 -> "0"
        val t2 = 0 -> "1" -> "2"
        println(t2)
        println(t2._1._1)

        // 对偶Tuple
        val t3 = ("1","2")
        println(t3.swap)

        val array12 = Array[Int](1,2,3,4,5,6,7,8,9,10)

        // 入参 array
        // 结果 max min avg

    }

    def method(array: Array[Int]): Unit = {
            (array.max, array.min, array.sum/array.size)
        }
}
